<?php
namespace app\index\service;
use think\Db;

class ExportService
{
    /**
	 * 导出收支明细
     * @param array $id
	 * @param array $mid
     * @return array
     */
    public static function export_point_detail($id,$mid)
    {
    	$filename = '收支明细列表';
    	$titles = array(
    		array('code','账单编号'),
            array('point','点数'),                       
            array('type','类型'),          
            array('desc','服务说明'),
			array('create_at','创建时间')
        );
    	$list = Db::name('system_point_detailed')->where(['id'=>$id,'mid'=>$mid])->order('id desc')->select();
		if(!empty($list) && $list){			
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				if(in_array($v['type'], [1,3])){
					$arr[$k]['point'] = '+'.$v['point'];
				}else{
					$arr[$k]['point'] = '-'.$v['point'];
				}
				if($v['type'] == 1){
					$arr[$k]['type'] = '充值';
				}	
				if($v['type'] == 2 || $v['type'] == 4){
					$arr[$k]['type'] = '已使用';
				}
				if($v['type'] == 3){
					$arr[$k]['type'] = '已退回';
				}	
				$arr[$k]['desc'] = $v['desc'];	    		
				$arr[$k]['create_at'] = date('Y-m-d H:i',$v['create_at']);
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }


	/**
     * 导出社保服务--公司
     * @param array $id
	 * @param array $company_id
     * @return array
     */
    public static function export_handle_company($id,$company_id)
    {
    	$filename = '公司社保服务列表';
    	$titles = array(
    		array('code','服务编号'),                    
            array('service_name','服务名称'),                    
            array('create_at','委托日期'),
            array('status','状态')  
        );
    	$list = Db::name('system_service_handle')->where(['id'=>$id,'company_id'=>$company_id,'is_deleted'=>0])->order('id desc')->select();
		if(!empty($list) && $list){
			$service = Db::name('system_service')->column('title','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['service_name'] = $service[$v['service_id']];				
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));					
				if($v['status'] == 1 || $v['status'] == 2){
					$arr[$k]['status'] = '办理中';
				}
				if($v['status'] == 3){
					$arr[$k]['status'] = '已办理';
				}
				if($v['status'] == 4){
					$arr[$k]['status'] = '无法办理';
				}
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }


	/**
	 * 导出社保服务--员工
     * @param array $id
	 * @param array $company_id
     * @return array
     */
    public static function export_handle_person($id,$company_id)
    {
    	$filename = '员工社保服务列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),                       
            array('id_number','身份证号'),
            array('service_name','服务名称'),
			array('create_at','委托日期'),
            array('status','状态')           
        );
    	$list = Db::name('system_service_handle')->where(['id'=>$id,'company_id'=>$company_id,'is_deleted'=>0])->order('id desc')->select();
		if(!empty($list) && $list){
			$service = Db::name('system_service')->column('title','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
				$arr[$k]['name'] = $v['name'];
				$arr[$k]['id_number'] = ' '.$v['id_number'];
				$arr[$k]['service_name'] = $service[$v['service_id']];
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
				if($v['status'] == 1 || $v['status'] == 2){
					$arr[$k]['status'] = '办理中';
				}
				if($v['status'] == 3){
					$arr[$k]['status'] = '已办理';
				}
				if($v['status'] == 4){
					$arr[$k]['status'] = '无法办理';
				}				
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }


	/**
     * 导出减员
     * @param array $id
	 * @param array $company_id
     * @return array
     */
    public static function export_downsizing($id,$company_id)
    {
    	$filename = '减员服务列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','手机号码'),
            array('end_date','截止日期'),         
            array('status','审核状态'),
            array('create_at','委托时间')          
        );
    	$list = Db::name('system_downsizing')->where(['id'=>$id,'company_id'=>$company_id,'is_deleted'=>0])->order('id desc')->select();
		if(!empty($list) && $list){
			foreach($list as $k=>$v){
				$increase = Db::name('system_increase')->where(['id'=>$v['increase_id']])->field('name,id_number,mobile')->find();
	    		$arr[$k]['code'] = $v['code'];
	    		$arr[$k]['name'] = $increase['name'];
				$arr[$k]['id_number'] = ' '.$increase['id_number'];
				$arr[$k]['mobile'] = $increase['mobile'];			
	    		$arr[$k]['end_date'] = $v['end_date'];
				$arr[$k]['status'] = ['1'=>'待审核','2'=>'已审核','3'=>'已反馈'][$v['status']];
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    /**
     * 导出减员确认
     * @param array $id
	 * @param array $company_id
     * @return array
     */
    public static function export_downconfig($id,$company_id)
    {
    	$filename = '减员确认列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','手机号码'),
            array('insurance','险种类型'),
            array('end_date','截止日期'), 
            array('account_period','所属账期'),         
            array('status','确认状态'),
            array('confirm_date','确认时间')          
        );
    	$list = Db::name('system_downsizing_insurance')->where(['id'=>$id,'company_id'=>$company_id,'is_deleted'=>0])->order('id desc')->select();
		if(!empty($list) && $list){
			foreach($list as $k=>$v){
				$increase = Db::name('system_increase')->where(['id'=>$v['increase_id']])->field('name,id_number,mobile')->find();
	    		$arr[$k]['code'] = $v['code'];
	    		$arr[$k]['name'] = $increase['name'];
				$arr[$k]['id_number'] = ' '.$increase['id_number'];
				$arr[$k]['mobile'] = $increase['mobile'];
				$arr[$k]['insurance'] = get_option('insurance')[$v['insurance']];			
	    		$arr[$k]['end_date'] = $v['end_date'];
	    		$arr[$k]['account_period'] = $v['account_period'];
				$arr[$k]['status'] = ['0'=>'待审核 ','1'=>'待确认','2'=>'已确认','3'=>'已反馈'][$v['status']];
				$arr[$k]['confirm_date'] = $v['confirm_date'] ? date('Y-m-d',$v['confirm_date']) : '';
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }

	
	/**
     * 导出增员
     * @param array $id
	 * @param array $company_id
     * @return array
     */
    public static function export_increase($id,$company_id)
    {
    	$filename = '增员服务列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','手机号码'),
            array('start_date','起始时间'),
            array('wage','社保工资'),
            array('archives','托管档案'),                                 
            array('create_at','委托时间'),
 			array('status','审核状态')
        );
    	$list = Db::name('system_increase')->where(['id'=>$id,'company_id'=>$company_id,'is_deleted'=>0])->order('id desc')->select();
		if(!empty($list) && $list){
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
	    		$arr[$k]['name'] = $v['name'];
				$arr[$k]['id_number'] = ' '.$v['id_number'];
				$arr[$k]['mobile'] = $v['mobile'];
	    		$arr[$k]['start_date'] = $v['start_date'];
				$arr[$k]['wage'] = $v['wage'];
				$arr[$k]['archives'] = $v['archives'] == 1 ? '是' : '否';				
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
				$arr[$k]['status'] = ['1'=>'待审核','2'=>'已审核','3'=>'已反馈'][$v['status']];
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
    
    /**
     * 导出增员确认
     * @param array $id
	 * @param array $company_id
     * @return array
     */
    public static function export_incconfig($id,$company_id)
    {
    	$filename = '增员确认列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','电话号码'),                     
            array('insurance','保险类型'),     
            array('wage','月发工资'),
            array('company_base','单位基数'),
            array('personal_base','个人基数'),
            array('company_prop','单位比例（%）'),
            array('person_prop','个人比例（%）'),
            array('start_date','起始日期'), 
            array('account_period','所属账期'),  
            array('status','状态'),           
            array('confirm_date','确认日期')
        );		
		$list = Db::name('system_increase_insurance')->where(['id'=>$id,'company_id'=>$company_id,'is_deleted'=>0])->order('increase_id desc,id asc')->select();
		if(!empty($list) && $list){
			foreach($list as $k=>$v){
				$increase = Db::name('system_increase')->where(['id'=>$v['increase_id']])->find();
				$contract_insurance = Db::name('system_contract_insurance')->where(['contract_id'=>$v['contract_id'],'insurance'=>$v['insurance']])->find();
	    		$arr[$k]['code'] = $increase['code'];
	    		$arr[$k]['name'] = $increase['name'];
				$arr[$k]['id_number'] = ' '.$increase['id_number'];
				$arr[$k]['mobile'] = $increase['mobile'];
				$arr[$k]['insurance'] = get_option('insurance')[$v['insurance']];
				$arr[$k]['wage'] = $v['wage'];
				$arr[$k]['company_base'] = $v['company_base'];
	    		$arr[$k]['personal_base'] = $v['personal_base'];
	    		$arr[$k]['company_prop'] = $contract_insurance['company_prop'];
	    		$arr[$k]['person_prop'] = $contract_insurance['person_prop'];
				$arr[$k]['start_date'] = $v['start_date'];
				$arr[$k]['account_period'] = $v['account_period'];
				$arr[$k]['status'] = ['1'=>'待审核 ','2'=>'待确认','3'=>'已确认','4'=>'已反馈'][$v['status']];
				$arr[$k]['confirm_date'] = $v['confirm_date'] ? date('Y-m-d',$v['confirm_date']) : '';
	    	}
		}else{
			$arr = [];
		}    
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }

	
	/**
     * 导出员工
     * @param array $id
	 * @param array $company_id
     * @return array
     */
    public static function export_staff($id,$company_id)
    {
    	$filename = '员工列表';
    	$titles = array(
    		array('code','员工编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','手机号码'),                               
 			array('status','状态')
        );
    	$list = Db::name('system_increase')->where(['id'=>$id,'company_id'=>$company_id,'is_deleted'=>0])->order('id desc')->select();
		if(!empty($list) && $list){
			foreach($list as $k=>$v){
				$company_person = Db::name('system_company_person')->where(['increase_id'=>$v['id'],'company_id'=>$company_id])->find();
	    		$arr[$k]['code'] = $v['code'];
	    		$arr[$k]['name'] = $v['name'];
				$arr[$k]['id_number'] = ' '.$v['id_number'];
				$arr[$k]['mobile'] = $v['mobile'];
				if($company_person['status'] == 1){
					$arr[$k]['status'] = '在职';
				}
				if($company_person['status'] == 2){
					$arr[$k]['status'] = '解约';
				}
				if($company_person['status'] == 3){
					$arr[$k]['status'] = '离职';
				}				
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
}